package com.cskaoyan.service;

import com.cskaoyan.bean.PO.MarketSystem;
import com.cskaoyan.bean.PO.MarketSystemExample;
import com.cskaoyan.bean.VO.AdminConfigExpressVo;
import com.cskaoyan.bean.VO.AdminConfigMallVo;
import com.cskaoyan.bean.VO.AdminConfigOrderVo;
import com.cskaoyan.bean.VO.AdminConfigWxVo;
import com.cskaoyan.mapper.MarketSystemMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

/**
 * @Description:配置管理
 * @author:刘杰
 * @create: 2022-08-14 22:38
 */
@Service
public class AdminConfigServiceImpl implements AdminConfigService{

    @Autowired
    MarketSystemMapper marketSystemMapper;

    @Override
    public AdminConfigMallVo mall() {
        MarketSystemExample example = new MarketSystemExample();
        MarketSystemExample.Criteria criteria = example.createCriteria();
        criteria.andKeyNameIn(Arrays.asList("market_mall_longitude","market_mall_latitude","market_mall_address","market_mall_qq","market_mall_phone","market_mall_name"));
        List<MarketSystem> marketSystems = marketSystemMapper.selectByExample(example);

        HashMap<String, String> map = new HashMap<>();
        for (MarketSystem marketSystem : marketSystems) {
            map.put(marketSystem.getKeyName(),marketSystem.getKeyValue());
        }

        AdminConfigMallVo orderVo = new AdminConfigMallVo();
        orderVo.setMarket_mall_longitude(map.get("market_mall_longitude"));
        orderVo.setMarket_mall_latitude(map.get("market_mall_latitude"));
        orderVo.setMarket_mall_address(map.get("market_mall_address"));
        orderVo.setMarket_mall_qq(map.get("market_mall_qq"));
        orderVo.setMarket_mall_phone(map.get("market_mall_phone"));
        orderVo.setMarket_mall_name(map.get("market_mall_name"));
        return orderVo;
    }

    @Override
    public AdminConfigExpressVo express() {
        MarketSystemExample example = new MarketSystemExample();
        MarketSystemExample.Criteria criteria = example.createCriteria();
        criteria.andKeyNameIn(Arrays.asList("market_express_freight_min","market_express_freight_value"));
        List<MarketSystem> marketSystems = marketSystemMapper.selectByExample(example);

        HashMap<String, String> map = new HashMap<>();
        for (MarketSystem marketSystem : marketSystems) {
            map.put(marketSystem.getKeyName(),marketSystem.getKeyValue());
        }

        AdminConfigExpressVo expressVo = new AdminConfigExpressVo();
        expressVo.setMarket_express_freight_min(map.get("market_express_freight_min"));
        expressVo.setMarket_express_freight_value(map.get("market_express_freight_value"));
        return expressVo;
    }

    @Override
    public void updatemall(AdminConfigMallVo orderVo) {
        HashMap<String, String> map = new HashMap<>();
        map.put("market_mall_longitude",orderVo.getMarket_mall_longitude());
        map.put("market_mall_latitude",orderVo.getMarket_mall_latitude());
        map.put("market_mall_address", orderVo.getMarket_mall_address());
        map.put("market_mall_qq", orderVo.getMarket_mall_qq());
        map.put("market_mall_phone", orderVo.getMarket_mall_phone());
        map.put("market_mall_name", orderVo.getMarket_mall_name());
        Set<String> strings = map.keySet();
        for (String string : strings) {
            marketSystemMapper.update(string,map.get(string));
        }
    }

    @Override
    public void updateexpress(AdminConfigExpressVo adminConfigExpressVod) {
        HashMap<String, String> map = new HashMap<>();
        map.put("market_express_freight_min",adminConfigExpressVod.getMarket_express_freight_min());
        map.put("market_express_freight_value",adminConfigExpressVod.getMarket_express_freight_value());
        Set<String> strings = map.keySet();
        for (String string : strings) {
            marketSystemMapper.update(string,map.get(string));
        }
    }

    @Override
    public AdminConfigOrderVo order() {
        MarketSystemExample example = new MarketSystemExample();
        MarketSystemExample.Criteria criteria = example.createCriteria();
        criteria.andKeyNameIn(Arrays.asList("market_order_unconfirm","market_order_unpaid","market_order_comment"));
        List<MarketSystem> marketSystems = marketSystemMapper.selectByExample(example);

        HashMap<String, String> map = new HashMap<>();
        for (MarketSystem marketSystem : marketSystems) {
            map.put(marketSystem.getKeyName(),marketSystem.getKeyValue());
        }

        AdminConfigOrderVo orderVo = new AdminConfigOrderVo();
        orderVo.setMarket_order_unconfirm(map.get("market_order_unconfirm"));
        orderVo.setMarket_order_unpaid(map.get("market_order_unpaid"));
        orderVo.setMarket_order_comment(map.get("market_order_comment"));
        return orderVo;
    }

    @Override
    public void updateOrder(AdminConfigOrderVo orderVo) {
        HashMap<String, String> map = new HashMap<>();
        map.put("market_order_unconfirm",orderVo.getMarket_order_unconfirm());
        map.put("market_order_unpaid",orderVo.getMarket_order_unpaid());
        map.put("market_order_comment", orderVo.getMarket_order_comment());
        Set<String> strings = map.keySet();
        for (String string : strings) {
            marketSystemMapper.update(string,map.get(string));
        }
    }

    @Override
    public AdminConfigWxVo wx() {
        MarketSystemExample example = new MarketSystemExample();
        MarketSystemExample.Criteria criteria = example.createCriteria();
        criteria.andKeyNameIn(Arrays.asList("market_wx_index_new","market_wx_index_topic","market_wx_index_brand","market_wx_catlog_goods","market_wx_catlog_goods","market_wx_catlog_list","market_wx_index_hot"));
        List<MarketSystem> marketSystems = marketSystemMapper.selectByExample(example);

        HashMap<String, String> map = new HashMap<>();
        for (MarketSystem marketSystem : marketSystems) {
            map.put(marketSystem.getKeyName(),marketSystem.getKeyValue());
        }

        AdminConfigWxVo wxVo = new AdminConfigWxVo();
        wxVo.setMarket_wx_index_new(map.get("market_wx_index_new"));
        wxVo.setMarket_wx_index_topic(map.get("market_wx_index_topic"));
        wxVo.setMarket_wx_share(map.get("market_wx_share"));
        wxVo.setMarket_wx_index_brand(map.get("market_wx_index_brand"));
        wxVo.setMarket_wx_catlog_goods(map.get("market_wx_catlog_goods"));
        wxVo.setMarket_wx_catlog_list(map.get("market_wx_catlog_list"));
        wxVo.setMarket_wx_index_hot(map.get("market_wx_index_hot"));
        return wxVo;
    }

    @Override
    public void updateWx(AdminConfigWxVo wxVo) {
        HashMap<String, String> map = new HashMap<>();
        map.put("market_wx_index_new",wxVo.getMarket_wx_index_new());
        map.put("market_wx_index_topic",wxVo.getMarket_wx_index_topic());
        map.put("market_wx_share",wxVo.getMarket_wx_share());
        map.put("market_wx_index_brand",wxVo.getMarket_wx_index_brand());
        map.put("market_wx_catlog_goods",wxVo.getMarket_wx_catlog_goods());
        map.put("market_wx_catlog_list",wxVo.getMarket_wx_catlog_list());
        map.put("market_wx_index_hot",wxVo.getMarket_wx_index_hot());
        Set<String> strings = map.keySet();
        for (String string : strings) {
            if(map.get(string)!=null) {
                marketSystemMapper.update(string, map.get(string));
            }
        }
    }
}
